Loading packages for the plots

library(ggplot2)
library(plotly)
library(flexdashboard)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## Warning: package 'tibble' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'purrr' was built under R version 4.1.3
## Warning: package 'stringr' was built under R version 4.1.3
## Warning: package 'forcats' was built under R version 4.1.3
## Warning: package 'lubridate' was built under R version 4.1.3
library(leaflet)

Reading in Cleaned Data

alcohol_data_2007 = read_csv("./data/PRAM_2007_alcohol.csv")

tobacco_data_2007 = read_csv("./data/PRAM_2007_tobacco.csv")

no_contraception_data_2007 = read_csv("./data/PRAM_2007_no_contraception.csv")

infant_mortality_df = read_csv("./data/PRAM_2007_infantmortality.csv")

# cleaned alcohol data 
cleaned_alc_2007 <- alcohol_data_2007 |>
  janitor::clean_names() |>
  select(-data_value_std_err, -data_value_type) |>
  filter(response != "DRINKER WHO QUIT") |>
  filter(response != "NONDRINKER") |>
  filter( response != "NO") |>
  drop_na(response,geolocation) |>
  separate(geolocation, into = c("latitude", "longitude"), sep = ", ", convert = TRUE) |>
   mutate(latitude = as.numeric(str_replace_all(latitude, "\\(|\\)", "")),  # Convert to numeric and remove parentheses
         longitude = as.numeric(str_replace_all(longitude, "\\(|\\)", "")))  # Convert to numeric and remove parentheses

# cleaned tobacco data 

cleaned_tobac_2007 <- tobacco_data_2007 |>
  janitor::clean_names() |>
  select(-data_value_type) |>
  filter(response != "SMOKER WHO QUIT") |>
  filter(response != "NONSMOKER") |>
  filter(response != "None (0 cig)") |>
  filter( response != "NO") |>
  drop_na(response, geolocation) |>
  separate(geolocation, into = c("latitude", "longitude"), sep = ", ", convert = TRUE) |>
   mutate(latitude = as.numeric(str_replace_all(latitude, "\\(|\\)", "")),  # Convert to numeric and remove parentheses
         longitude = as.numeric(str_replace_all(longitude, "\\(|\\)", "")))  # Convert to numeric and remove parentheses
  
no_alcohol_data_2007 = read_csv("./data/PRAM_2007_no_alcohol.csv")

no_tobacco_data_2007 = read_csv("./data/PRAM_2007_no_tobacco.csv")

contraception_data_2007 = read_csv("./data/PRAM_2007_contraception.csv")

# cleaned no alcohol data 

cleaned_no_alc_2007 <- no_alcohol_data_2007 |>
  janitor::clean_names() |>
  select(-data_value_std_err, -geolocation, -data_value_type) |>
  drop_na(response)

view(cleaned_no_alc_2007)

# cleaned no tobacco data 

cleaned_no_tobacco_2007 <- no_tobacco_data_2007 |>
  janitor::clean_names() |>
  select(-data_value_std_err, -geolocation, -data_value_type) |>
  drop_na(response)

# cleaned infant mortality 

cleaned_infant_mortality <- infant_mortality_df |>
  janitor::clean_names() |>
  select(-data_value_std_err, -data_value_type, -data_value_unit, -data_value_footnote_symbol, -data_value_footnote) |>
  drop_na(response, geolocation) |>
  separate(geolocation, into = c("latitude", "longitude"), sep = ", ", convert = TRUE) |>
   mutate(latitude = as.numeric(str_replace_all(latitude, "\\(|\\)", "")),  # Convert to numeric and remove parentheses
         longitude = as.numeric(str_replace_all(longitude, "\\(|\\)", "")))  # Convert to numeric and remove parentheses

# cleaned conception

cleaned_contraception_2007 <- contraception_data_2007 |>
  janitor::clean_names() |>
  select(-data_value_std_err, -geolocation, -data_value_type) |>
  filter(response != "YES (CHECKED)") |>
  filter(response != "YES") |>
  drop_na(response)

# cleaned non conception

cleaned_no_contra_2007 <- no_contraception_data_2007 %>%
  janitor::clean_names() %>%
  select(-data_value_type) %>%
  drop_na(response) |>
  separate(geolocation, into = c("latitude", "longitude"), sep = ", ", convert = TRUE) |>
   mutate(latitude = as.numeric(str_replace_all(latitude, "\\(|\\)", "")),  # Convert to numeric and remove parentheses
         longitude = as.numeric(str_replace_all(longitude, "\\(|\\)", "")))  # Convert to numeric and remove parentheses

Plot 1: Alcohol Consumption in relation to Infant Mortality

cleaned_infant_mortality <- infant_mortality_df |>
  janitor::clean_names() |>
  select(-data_value_std_err, -data_value_type, -data_value_unit, -data_value_footnote_symbol, -data_value_footnote) |>
  drop_na(response)

# Plot of question and responses for alcohol

cleaned_alc_2007 |>
  ggplot(aes(x = question, fill = response)) +
  geom_bar(position = "dodge") +
  labs(title = "Questions and Responses", x = "Questions", y = "Count") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  labs(
    x = "Question",
    y = "Response",
    title =  "Questions vs Response of Alcohol Consumption"
  )

# creating "yes" variable 


# plot showing infant mortality rate vs alcohol consumption
ggplot() +
  geom_point(data = cleaned_alc_2007, aes(x = question, y = response), color = "blue", size = 3) +
  geom_point(data = cleaned_infant_mortality, aes(x = question, y = response), color = "red", size = 3) +
  labs(title = "Scatter Plot of Two Variables from Different Datasets",
       x = "X-axis Label",
       y = "Y-axis Label") +
  theme_minimal()

Plot 2: Tobacco Consumption in relation to Infant Mortality

Plot 3: No Consumption in relation to Infant Mortality

Map of Maternal Alcohol Use

leaflet() |> 
  addTiles() |> 
  addCircleMarkers(data = cleaned_alc_2007,
                   lng = ~longitude,  # Adjust column name if needed
                   lat = ~latitude,   # Adjust column name if needed
                   label = ~location_abbr,   # Assuming 'Group.1' is a column in your data
                   radius = 7,
                   color = "orange",
                   stroke = TRUE,
                   fillOpacity = 0.75,
                   popup = ~paste("Response:", response)) 
<<<<<<< HEAD <<<<<<< HEAD
======= <<<<<<< HEAD
=======
>>>>>>> 94e7f9778c789b861d08491901c0e1020c6aa653 >>>>>>> 87eda6801483a374e516ba8f783995c3d11f822b =======
>>>>>>> d62d042cf80b91ce83fdee3be5c3dcaae96e5cba

Map of Maternal Tobacco use

leaflet() |> 
  addTiles() |> 
  addCircleMarkers(data = cleaned_tobac_2007,
                   lng = ~longitude,  # Adjust column name if needed
                   lat = ~latitude,   # Adjust column name if needed
                   label = ~location_abbr,   # Assuming 'Group.1' is a column in your data
                   radius = 7,
                   color = "orange",
                   stroke = TRUE,
                   fillOpacity = 0.75,
                   popup = ~paste("Response:", response)) 
<<<<<<< HEAD <<<<<<< HEAD
======= <<<<<<< HEAD
## Warning in validateCoords(lng, lat, funcName): Data contains 1 rows with either
## missing or invalid lat/lon values and will be ignored
=======
>>>>>>> 94e7f9778c789b861d08491901c0e1020c6aa653 >>>>>>> 87eda6801483a374e516ba8f783995c3d11f822b =======
>>>>>>> d62d042cf80b91ce83fdee3be5c3dcaae96e5cba

Map of Infant Mortality Rate